package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConst;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserDao;
import com.itheima.entity.Result;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import com.itheima.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

	@Autowired
	UserDao userDao;



	@Override
	public boolean login(String username, String password) {
		log.debug("service_provide...u:"+username+" p:"+password);
		if("admin".equals(username) && "123".equals(password)){
			return true;
		}else{
			throw new BusinessRuntimeException("登录失败!!!");
		}
	}

	@Override
	public SysUser findByUsername(String username) {
		return userDao.findByUsername(username);
	}

	@Override
	public Result getUserMenuByUsername(String username) {
		ArrayList<Object> list = new ArrayList<>();
		// 1.查询当前角色所能够查看的一级菜单
		List<Menu> menu1List = userDao.find1MenuByUsername(username);
		log.debug(JSON.toJSONString(menu1List));
		// 便利一级菜单
		for (Menu menu : menu1List) {
			HashMap<String, Object> map = new HashMap<>();
			Integer id = menu.getId();
			// 根据一级菜单的id，查询出当前用户可访问的对应的二级菜单
			List<Menu> menu2List = userDao.find2MenuBy1Id(id, username);
			// 遍历二级菜单
			ArrayList<Object> list2 = new ArrayList<>();
			for (Menu menu2 : menu2List) {
				HashMap<String, Object> map2 = new HashMap<>();
				map2.put("path", menu2.getPath());
				map2.put("title", menu2.getName());
				map2.put("linkUrl", menu2.getLinkUrl());
				list2.add(map2);
			}
			// 封装数据
			map.put("path", menu.getPath());
			map.put("title", menu.getName());
			map.put("icon", menu.getIcon());
			map.put("children", list2);
			list.add(map);
		}
		log.debug("getUserMenuByUsername执行完成" + JSON.toJSONString(list));
		return new Result(true, MessageConst.GET_MENU_SUCCESS, list);
	}

	@Override
	public void add(SysUser sysuser, Integer[] roleIds) {
		userDao.add(sysuser);
		//2. 维护中间表的数据（检查组和检查项的关系）
		if(sysuser.getId() != null){
			setRelation(sysuser.getId(), roleIds);
		}
	}

	public void setRelation(Integer userId ,Integer[] roleId){
		if(userId != null && roleId != null && roleId.length > 0){
			for (Integer roleIds : roleId) {
				userDao.set(userId,roleIds);
			}
		}
	}

	@Override
    public PageResult findPage(QueryPageBean queryPageBean) {
    log.debug("service:"+queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<SysUser> page = userDao.findByCondition(queryPageBean.getQueryString());
        log.debug("page{"+page+"}");
        return new PageResult(page.getTotal(),page);
    }

	@Override
	public void delById(Integer id) {
		long count = userDao.findCountById(id);
		if(count > 0) {
			throw new RuntimeException("该用户被角色关联，不能删除！！");
		}else {
			userDao.delById(id);
		}
	}

	@Override
	public void edit(SysUser sysuser,Integer[] roleIds) {
		userDao.edit(sysuser);
		userDao.delRelation(sysuser.getId());
		if(sysuser.getId() != null){
			setRelation(sysuser.getId(), roleIds);
		}
	}

	@Override
	public SysUser findById(Integer id) {
		return userDao.findById(id);
	}

	@Override
	public List<Integer> findRoleIdsById(Integer id) {
		return userDao.findRoleIdsById(id);
	}
}